<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <title>管理系统</title>
    <meta charset="utf-8">
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"/>
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="renderer" content="webkit">
    <link rel="stylesheet" href="/src/plugs/layui/2.4.5/css/layui.css">
    <link rel="stylesheet" href="/src/plugs/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/src/css/global.css">
    <script type="application/javascript" src="/src/plugs/jquery/dist/jquery.min.js"></script>
</head>
<body>
<div class="layui-layout layui-layout-admin">
    <div class="layui-header">
        <div class="layui-logo">TPR 后台管理系统</div>
        <!-- 头部区域（可配合layui已有的水平导航） -->
        <ul class="layui-nav layui-layout-left">
            <li class="layui-nav-item"><a href="javascript:void(0);" onclick="refresh()">刷新</a></li>
            <li class="layui-nav-item">
                <a href="javascript:void(0);" class="menu" data-id="api-test" data-url="api/index/index"
                   data-icon="fa fa-cloud" data-title="接口管理">接口管理</a>
            </li>
            <li class="layui-nav-item">
                <a href="javascript:void(0);">快捷链接</a>
                <dl class="layui-nav-child">
                    <dd><a href="https://www.google.com" target="_blank">谷歌</a></dd>
                    <dd><a href="https://www.baidu.com" target="_blank">百度</a></dd>
                    <dd><a href="https://www.bing.com" target="_blank">必应</a></dd>
                </dl>
            </li>
        </ul>
        <ul class="layui-nav layui-layout-right">
            <li class="layui-nav-item">
                <a href="javascript:void(0);" id="profile">
                    <img src="{{ user_info.avatar }}" class="layui-nav-img" alt="头像">
                </a>
            </li>
            <li class="layui-nav-item"><a href="{{ url('index','index','logout') }}">退出</a></li>
        </ul>
    </div>
    {% if menu %}
    <div class="layui-side layui-bg-black">
        <div class="layui-side-scroll">
            <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
            <ul class="layui-nav layui-nav-tree" id="nav" lay-filter="menu">
                {% for m in menu %}
                <li class="layui-nav-item{{ selected(m['module'],module) ? ' layui-nav-itemed':'' }}">
                    <a class="menu menu-parent" href="javascript:void(0);" data-id="{{ m['id'] }}"
                       data-url="{{ url(m['module'],m['controller'],m['action']) }}" data-icon="fa fa-{{ m['icon'] }}"
                       data-title="{{ m['title'] }}">
                        <i class="fa fa-{{ m['icon'] }}" data-icon="fa fa-{{ m['icon'] }}"></i>
                        <span>{{ m['title'] }}</span>
                    </a>
                    {% set sub = m['sub'] %}
                    {% if sub %}
                    <dl class="layui-nav-child">
                        {% for s in sub %}
                        <dd>
                            <a class="menu menu-child" href="javascript:void(0);" data-id="{{ s['id'] }}"
                               data-url="{{ url(s['module'],s['controller'],s['action']) }}"
                               data-icon="fa fa-{{ s['icon'] }}" data-title="{{ s['title'] }}">
                                <i class="fa fa-{{ s['icon'] }}" data-icon="fa fa-{{ s['icon'] }}"></i>
                                <span>{{ s['title'] }}</span>
                            </a>
                        </dd>
                        {% endfor %}
                    </dl>
                    {% endif %}
                </li>
                {% endfor %}
            </ul>
        </div>
    </div>
    {% endif %}
    <div class="layui-body" style="padding-left: 10px;padding-right: 10px;">
        <!-- 内容主体区域 -->
        <div class="layui-tab layui-tab-brief" lay-filter="iframe-tab" lay-allowClose="true">
            <ul class="layui-tab-title">
                <li class="layui-this" lay-id="home"><i class="fa fa-home"></i>后台首页</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <iframe id="tpr0" class="tpr-iframe" width="100%" onload="iframeHeight()" data-id="home"
                            src="{{ url('index','index','main') }}" frameborder="0"></iframe>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-footer">
        <div class="layui-mian">
            <div style="text-align: center">
                <a target="_blank"
                   href="//shang.qq.com/wpa/qunwpa?idkey=4b2b9c98132bed88a24e71f7f9d49030a39be51f5b15dfdab59cd9d17cc1b4e5">
                    <img border="0" src="//pub.idqqimg.com/wpa/images/group.png" alt="TPR-CMS" title="TPR-CMS"></a>
                <a href="https://github.com/AxiosCros/tpr-cms" title="" target="_blank">TPR CMS</a> ·
                前端框架<a href="http://www.layui.com/demo/">layui</a>
            </div>
        </div>
    </div>
</div>
<script src="/src/plugs/layui/2.4.5/layui.all.js"></script>
<script>
    let element;
    let current_tab;

    //JavaScript代码区域
    layui.use('element', function () {
        element = layui.element;
        element.on("tab(iframe-tab)", function (data) {
            current_tab = data.index;
        });
    });

    $(window).resize(function () {
        iframeHeight();
    });

    function iframeHeight() {
        const lay_body = $(".layui-body");
        const height = lay_body.height() - 86;
        $('iframe').height(height);
    }

    const nav = $(".menu");

    nav.click(function () {
        const obj = $(this).parent();
        const hasChild = obj.find('dd').length;

        if (!hasChild) {
            tabAdd(this);
        }
    });
    $("#profile").click(function () {
        const title = '基本资料';
        const url = "/user/index/profile";
        const icon = "fa fa-user";
        const id = 3;
        element.tabDelete('iframe-tab', id);
        const content = '<iframe id="tpr' + id + '" class="tpr-iframe"  width="100%" onload="iframeHeight()" data-id="0" src="' + url + '" frameborder="0" ></iframe>';
        element.tabAdd('iframe-tab', {
            title: '<i class="' + icon + '"></i>' + title,
            href: content,
            id: id
        });
        element.tabChange('iframe-tab', id);
        const show = $(".layui-show");
        show.empty();
        show.append(content);
    });

    function tabAdd(obj) {
        const $obj = $(obj);
        const id = $obj.attr('data-id');

        const exist = $(".layui-tab").find('iframe[data-id=' + id + ']');

        if (exist.length) {
            console.log(id);
            element.tabChange('iframe-tab', id);
        } else {
            const title = $obj.attr('data-title');
            const url = $obj.attr('data-url');
            const icon = $obj.attr('data-icon');
            element.tabDelete('iframe-tab', id);
            const content = '<iframe id="tpr' + id + '" class="tpr-iframe"  width="100%" onload="iframeHeight()" data-id="' + id + '" src="' + url + '" frameborder="0" ></iframe>';
            element.tabAdd('iframe-tab', {
                title: '<i class="' + icon + '"></i>' + title,
                href: content,
                id: id
            });
            element.tabChange('iframe-tab', id);
            const show = $(".layui-show");
            show.empty();
            show.append(content);
        }
    }

    function refresh() {
        const iframe = $(".layui-show").find('iframe');
        const src = iframe.attr('src');
        iframe.attr('src', src);
    }
</script>
</body>
</html>
